/*
 *
 * Copyright 2011, Ibrahim Arief
 * 
 * This file is part of Bambu Game Backend.
 *
 * Bambu Game Backend is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Bambu Game Backend is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Bambu Game Backend.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package com.appspot.bambugame.server.rest;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import com.appspot.bambugame.server.PlurkService;
import com.appspot.bambugame.server.data.ScramblePlurkAnswerers;
import com.google.appengine.api.datastore.QueryResultIterable;
import com.google.appengine.repackaged.com.google.common.collect.Lists;
import com.google.jplurk.Qualifier;
import com.google.jplurk.exception.PlurkException;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

public class HangmanTopScoreCronServlet extends HttpServlet
{
    /**
     * 
     */
    private static final long serialVersionUID = -8957362320144318364L;
    
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            // get players
            List<ScramblePlurkAnswerers> tTopTenPlurkers = getTopNScorer( 10 );
            
            JSONObject tAddResponse = PlurkService.getInstance().plurkAdd( "Top 10 plurkers for the past week (updated every 3 hours) :", Qualifier.SHARES );
            long tNewPlurkID = tAddResponse.getLong( "plurk_id" );
            
            //for (ScramblePlurkAnswerers tAnswerer : tTopTenPlurkers)
            for (int i = 0; i < tTopTenPlurkers.size(); i++)
            {
                ScramblePlurkAnswerers tAnswerer = tTopTenPlurkers.get( i );
                
                System.out.println("User with id " + tAnswerer.PLURK_USER_ID + " got " + tAnswerer.NUMBER_OF_CORRECT_ANSWERS);
                
                plurkTopScoreAnswerer(tNewPlurkID, i+1, tAnswerer);
            }
            
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            System.out.println("Exception " + ex.getClass() + ", " + ex.getMessage() + " : " + Arrays.deepToString( ex.getStackTrace() ));
        }
    }
    
    public void plurkTopScoreAnswerer(long pPlurkID, int pRank, ScramblePlurkAnswerers pAnswerer) throws PlurkException, JSONException
    {
        String tAnswererNickName = getAnswererNickName( pAnswerer );
        PlurkService.getInstance().responseAdd( String.valueOf(pPlurkID), pRank + ". @" + tAnswererNickName + ", " + (pAnswerer.NUMBER_OF_CORRECT_ANSWERS * 10) + " points.", Qualifier.SAYS );
    }
    
    public String getAnswererNickName(ScramblePlurkAnswerers pAnswerer) throws PlurkException, JSONException
    {
        if (pAnswerer.PLURK_USER_NAME != null && !pAnswerer.PLURK_USER_NAME.isEmpty()) return pAnswerer.PLURK_USER_NAME;
        
        JSONObject tProfileQueryResponse = PlurkService.getInstance().getPublicProfile( String.valueOf ( pAnswerer.PLURK_USER_ID ) );
        
        pAnswerer.PLURK_USER_NAME = tProfileQueryResponse.getJSONObject( "user_info" ).getString( "nick_name" );
        
        ObjectifyService.begin().put( pAnswerer );
        
        return pAnswerer.PLURK_USER_NAME;
    }
    
    public List<ScramblePlurkAnswerers> getTopNScorer(int pTopN)
    {
        Objectify tObjectify = ObjectifyService.begin();
        QueryResultIterable<ScramblePlurkAnswerers> tRetIter = tObjectify.query( ScramblePlurkAnswerers.class ).order( "-NUMBER_OF_CORRECT_ANSWERS" ).limit( pTopN ).fetch();
        
        return Lists.newArrayList( tRetIter );
    }
}
